﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Register 
{
    public partial class FrmDangky : DevExpress.XtraEditors.XtraForm
    {
        DAL.EntitesDAL.HocvienDAL _dal = new DAL.EntitesDAL.HocvienDAL();
        BLL.EntitiesBLL.HocvienBLL _bll = new BLL.EntitiesBLL.HocvienBLL();
        DataTable _dtb = new DataTable();

        DAL.EntitesDAL.LophocDAL _dal1 = new DAL.EntitesDAL.LophocDAL();
        DataTable _dtb1 = new DataTable();

        public static string _maHV;
        public static string _tenHV;
        public static long _soTien;

        public FrmDangky()
        {
            InitializeComponent();
        }

        private void FrmDangky_Load(object sender, EventArgs e)
        {
            EnableButton();
            ReadOnly();

            //if (_dtb != null)
            //{
            //    // Binding data
            //    ClearDataBindings();
            //    if (_dtb.Rows.Count > 0) DataBindingControl();
            //}

            _dtb = _dal.Select();
            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
            }

            _dtb1 = _dal1.Select();
            if (_dtb1 != null)
            {
                cboLop.Properties.DataSource = _dtb1;
            }
            
        }

        private void EnableButton(bool isTrue = true)
        {
            btnThem.Visible = isTrue;
            btnDangky.Visible = !isTrue;
            btnNhaplai.Visible = !isTrue;
        }

        private void ResetTest()
        {
            txtHoten.Text = null;
            txtDiachi.Text = null;
            txtDienthoai.Text = null;

            dteNamsinh.EditValue = DateTime.Now;
            cboLop.ItemIndex = -1;
            radioGioitinh.EditValue = true;

            txtHoten.Focus();
        }

        private void ReadOnly(bool isTrue = true)
        {
            txtHoten.Properties.ReadOnly = isTrue;
            txtDienthoai.Properties.ReadOnly = isTrue;
            txtDiachi.Properties.ReadOnly = isTrue;
            radioGioitinh.Properties.ReadOnly = isTrue;
            cboLop.Properties.ReadOnly = isTrue;
            dteNamsinh.Properties.ReadOnly = isTrue;
        }

        private bool TestInput()
        {
            var a = txtHoten.Text.Length == 0 ? false : true;
            if (!a) txtHoten.Focus();

            var b = txtDienthoai.Text.Length == 0 ? false : true;
            if (!b) txtDienthoai.Focus();

            var c = txtDiachi.Text.Length == 0 ? false : true;
            if (!c) txtDiachi.Focus();

            var d = Convert.ToDateTime(dteNamsinh.EditValue) > DateTime.Now ? false : true;
            if (!d) txtHoten.Focus();

            return a && b && c && d;
        }

        private string Add()
        {
            string kq="";
            DataTable _ma = _dal.Select_Max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    kq = _bll.AddID(_ma.Rows[0]["Ma"] + "");
                }
            return kq;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            EnableButton(false);
            ResetTest();
            ReadOnly(false);
            txtMaHV.Text = Add();
        }

        private void btnNhaplai_Click(object sender, EventArgs e)
        {
            ResetTest();
            ReadOnly(false);
            txtMaHV.Text = Add();
        }

        private void btnDangky_Click(object sender, EventArgs e)
        {
            Catalog.FrmBase x = new Catalog.FrmBase();
            var name = x.Proper(txtHoten.Text);

            if (TestInput())
            {
                var o = new DAL.Entities.Hocvien()
                {
                    MaHV = txtMaHV.Text,
                    Hoten = name,
                    Namsinh = Convert.ToDateTime(dteNamsinh.EditValue),
                    Gioitinh = Convert.ToBoolean(radioGioitinh.EditValue),
                    Diachi = txtDiachi.Text,
                    Dienthoai = txtDienthoai.Text,
                    MalopID = Convert.ToString(cboLop.EditValue)
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    //MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FrmDangky_Load(sender, e);
                    EnableButton(false);
                    ResetText();

                    _maHV = txtMaHV.Text;
                    _tenHV = name;
                    
                    // Lấy số tiền học phí
                    string _malop = Convert.ToString(cboLop.EditValue);
                    DataTable tmp = new DataTable();
                    tmp = _dal.Select_HP(_malop);
                    _soTien = Convert.ToInt64(tmp.Rows[0]["Hocphi"] + "");

                    FrmBienlai frm = new FrmBienlai();
                    frm._mahv = _maHV;
                    frm._tenhv = _tenHV;
                    frm._sotien = _soTien;
                    frm.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    EnableButton(false);
                    ReadOnly(false);
                }
            }
            else
                XtraMessageBox.Show("Dữ liệu sai, xin bạn hãy nhập lại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void ClearDataBindings()
        {
            txtMaHV.DataBindings.Clear();
            txtHoten.DataBindings.Clear();
            dteNamsinh.DataBindings.Clear();
            radioGioitinh.DataBindings.Clear();
            txtDiachi.DataBindings.Clear();
            txtDienthoai.DataBindings.Clear();
            cboLop.DataBindings.Clear();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        private void DataBindingControl()
        {
            txtMaHV.DataBindings.Add("EditValue", _dtb, ".MaHV");
            txtHoten.DataBindings.Add("EditValue", _dtb, ".HoTen");
            dteNamsinh.DataBindings.Add("EditValue", _dtb, ".Namsinh");
            radioGioitinh.DataBindings.Add("EditValue", _dtb, ".Gioitinh");
            txtDiachi.DataBindings.Add("EditValue", _dtb, ".Diachi");
            txtDienthoai.DataBindings.Add("EditValue", _dtb, ".Dienthoai");
            cboLop.DataBindings.Add("EditValue", _dtb, ".MalopID");
        }


        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            FrmDangky_Load(sender, e);
        }
    }
}